import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import {createStore, applyMiddleware} from 'redux'
import {Provider} from 'react-redux'

const reducer = (state, action) => {
  console.log(action, '11111')
  return state
}

// 当前定义一个中间件函数
const middle = ({ getState, dispatch }) => {
  console.log(getState, '===', dispatch)
  return (next) => {
    // 此处的action是接受的action
    return (action) => {
      // 此处可以完成自己的异步操作
      // 此处如果我们需要操作一些数据，然后往后边传递，那么就可以通过属性交给action
      action.payload = 123
      setTimeout( () => {
        // 注意：在此处的异步操作执行完毕以后，还是需要将action交给store继续往后传递。
        return next(action)
      }, 1000)
    }
  }
}

const store = createStore(reducer, applyMiddleware(middle))

store.dispatch({type: 'test'})

ReactDOM.render(
  <React.StrictMode>
    <Provider store={store}><App /></Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

